﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Viper;

namespace Viper.Opcodes
{
    public class push_d : Instruction
    {
        public push_d(uint Value)
        {
            this.Mnemonic = "push_d";
			this.Operand = Value.ToString();
			byte[] val = System.BitConverter.GetBytes(Value);
			this.Bytecode = new byte[]{1,4,val[0],val[1],val[2],val[3]};
        }
    }
	 public class push_f : Instruction
    {
        public push_f(float Value)
        {
            this.Mnemonic = "push_f";
			this.Operand = Value.ToString();
			byte[] val = System.BitConverter.GetBytes((float)(Math.Floor(Value * 1000000) / 1000000));
			this.Bytecode = new byte[]{1,4,val[0],val[1],val[2],val[3]};
        }
    }
	public class push_w : Instruction
    {
        public push_w(ushort Value)
        {
            this.Mnemonic = "push_w";
			this.Operand = Value.ToString();
			byte[] val = System.BitConverter.GetBytes(Value);
			this.Bytecode = new byte[]{1,2,val[0],val[1]};
        }
    }
	public class push_ptr : Instruction
    {
		private string sym = "";
		public int offset = 0;
        public push_ptr(string symbol)
        {
			this.Mnemonic = "push_ptr";
			this.Operand = symbol;
			this.Bytecode = new byte[]{1,4,0,0,0,0};
			sym = symbol;
			
        }
		public override void Relocate (ref List<Symbol> symbols)
		{
			uint address = 0;
			foreach(Symbol sb in symbols)
			{
				if(sb.Name == sym)
				{
					address = (uint)((int)sb.Address + offset);
					break;
				}
			}
			byte[] val = System.BitConverter.GetBytes(address);
			this.Bytecode = new byte[]{1,4,val[0],val[1],val[2],val[3]};
			
			Relocation r = new Relocation();
			r.Address = this.Address + 2;
			r.Symbol = sym;
			this.RelocData.Add(r);
		}
    }
	public class push_b : Instruction
    {
        public push_b(byte Value)
        {
            this.Mnemonic = "push_b";
			this.Operand = Value.ToString();
			this.Bytecode = new byte[]{1,1,Value};
        }
    }
}
